package com.example.content;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MyProvider_class extends AppCompatActivity {

    private Context mContext;  // 上下文
    DBHelper mDbHelper = null;  // 数据库帮助类
    SQLiteDatabase db = null;
    private Button btnUser, btnJob,button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnUser = findViewById(R.id.btn_user);
        btnJob = findViewById(R.id.btn_job);
        button3 = findViewById(R.id.button3);
        btnUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击操作User表的按钮
                operateUserTable();
            }
        });

        btnJob.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击操作Job表的按钮
                operateJobTable();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MyProvider_class.this,MainActivity.class);
                startActivity(intent);

            }
        });
    }

    private void operateUserTable() {
        // 设置URI
        Uri uri_user = Uri.parse("content://com.example.content.myprovider/user");
        // 插入数据
        ContentValues values = new ContentValues();
        values.put("_id", 3);
        values.put("name", "Iverson");
        // 获取ContentResolver
        ContentResolver resolver = getContentResolver();
        // 通过ContentResolver 根据URI 向ContentProvider中插入数据
        resolver.insert(uri_user, values);
        // 在 Activity 中显示一个短暂的 Toast 消息
        Toast.makeText(this, "user-sites!", Toast.LENGTH_SHORT).show();
    }

    private void operateJobTable() {
        // 设置URI
        Uri uri_job = Uri.parse("content://com.example.content.myprovider/job");
        // 插入数据
        ContentValues values = new ContentValues();
        values.put("_id", 3);
        values.put("job", "NBA Player");
        // 获取ContentResolver
        ContentResolver resolver = getContentResolver();
        // 通过ContentResolver 根据URI 向ContentProvider中插入数据
        resolver.insert(uri_job, values);
        // 在 Activity 中显示一个短暂的 Toast 消息
        Toast.makeText(this, "job-sites!", Toast.LENGTH_SHORT).show();
    }

}
